import React, {Component} from 'react';
import './index.less';
import {base} from 'nc-lightapp-front';
import Pagination from "src/hrpub/common/components/Pagination";
import {debounce} from "src/hrpub/common/utils/utils";

class AgreementTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableHeight: ''
        };
        this.changePage = this.changePage.bind(this);
        this.pageSizeChange = this.pageSizeChange.bind(this);
        this.rowDoubleClickHandle = this.rowDoubleClickHandle.bind(this);
        this.setTableHeight = this.setTableHeight.bind(this);
    }

    componentDidMount() {
        this.setTableHeight();
        window.addEventListener("resize", debounce(this.setTableHeight));
    }

    changePage(pageIndex) {
        const {
            updateState,
            getAgreeTableData,
            pageInfo,
            isQueryTemplate
        } = this.props;

        updateState({
            agreementPageInfo: {
                ...pageInfo,
                pageIndex: pageIndex
            }
        }, () => {
            getAgreeTableData(isQueryTemplate);
        });
    }

    rowDoubleClickHandle(record, index, props, e) {
        this.props.view('', record, index);
    }

    pageSizeChange(pageSize) {
        const {
            updateState,
            getAgreeTableData,
            pageInfo,
            isQueryTemplate
        } = this.props;

        updateState({
            agreementPageInfo: {
                ...pageInfo,
                pageSize,
                pageIndex: 1
            }
        }, () => {
            getAgreeTableData(isQueryTemplate);
        });
    }

    setTableHeight() {
        const content = document.getElementsByClassName('entry-staff-query-content');
        if (content[0]) {
            this.setState({
                tableHeight: content[0].clientHeight - 118
            })
        }
    }

    render() {
        const {table, pageInfo} = this.props;

        return (
            <div className='agree-table'>
                {this.state.tableHeight && table.createSimpleTable('agreementList', {
                    cancelCustomRightMenu: true,
                    showCheck: true,
                    showIndex: true,
                    height: this.state.tableHeight,
                    onRowDoubleClick: this.rowDoubleClickHandle
                })}
                {
                    pageInfo.total > 0 &&
                    <Pagination
                        current={parseInt(pageInfo.pageIndex)}
                        pageSize={parseInt(pageInfo.pageSize)}
                        pageSizeOptions={[5, 10, 20, 50, 100]}
                        showSizeChanger={true}
                        total={pageInfo.total}
                        onShowSizeChange={this.pageSizeChange}
                        onChange={this.changePage}
                    />
                }
            </div>
        );
    }
}

export default AgreementTable;